package com.chijun.mform.field.impl;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.chijun.mform.R;
import com.chijun.mform.create.mode.FormField;
import com.chijun.mform.field.BaseField;
import com.example.ccj.b.c.dialogfragment.scancode.ScanCodeDialogFragment;
import com.example.ccj.u.power.PowerPer;

import io.reactivex.ObservableEmitter;

/**
 * 扫码
 * @author 驰珺
 * whechat   : chijun9
 * create at : 2024/12/23 15:44
 */
public class FieldFor18SaoMa extends BaseField {
    public FieldFor18SaoMa(){
        super();
    }

    @Override
    public View getWritelView(Context context, WriteFormClass writeFormClass) {
        FormField formField = writeFormClass.formField;
        //提示文本
        if (formField.getHint() != null) writeFormClass.fieldFullContentView.setHint(formField.getHint());
        //输入文本颜色
        if (formField.getTextColor() != 0) writeFormClass.fieldFullContentView.setTextColor(formField.getTextColor());
        //默认文本
        String textFieldZanCunData = getTextFieldZanCunData(context,"fieldId=?",String.valueOf(formField.getId()));
        if (textFieldZanCunData == null) textFieldZanCunData = formField.getDefaultContent();
        if (textFieldZanCunData != null) {
            writeFormClass.fieldFullContentView.setText(textFieldZanCunData);
            saveValue = textFieldZanCunData;
            writeFormClass.inputIsOK = true;
        }

        //文本输入框不可输入
        writeFormClass.fieldFullContentView.setKeyListener(null);
        writeFormClass.fieldFullContentView.setFocusable(false);
        //跳转扫码界面
        writeFormClass.fieldFullJumpImgView.setVisibility(View.VISIBLE);
        writeFormClass.fieldFullJumpImgView.setImageResource(R.drawable.field_icon_saoma);
        writeFormClass.fieldFullJumpImgView.setOnClickListener(v -> {
            if (PowerPer.getScanCodePower(context)) {
                ScanCodeDialogFragment scanCodeDialogFragment = new ScanCodeDialogFragment(context, s -> {
                    writeFormClass.fieldFullContentView.setText(s);
                    saveValue = s;
                });
                scanCodeDialogFragment.show();
            }
        });
        return writeFormClass.view;
    }

    @Override
    protected View getShowFullView(Context context, FullClass fullModel) {
        //字段名属性设置
        fullModel.setFieldProperty();
        //设置字段值
        fullModel.setContentProperty();
        return fullModel.layout;
    }


    /**
     * 字段设置界面
     * @param context 上下文
     * @param installClass 配置
     * @return 1
     */
    @Override
    protected View getFieldInstallView(Context context, InstallClass installClass) {
        ObservableEmitter<ViewGroup> emitter = installClass.emitter;

        emitter.onNext(installClass.getIsMastView());        //是否必填
        emitter.onNext(installClass.getIsPrintView());        //是否参与打印

        return installClass.v;
    }

}
